import 'package:flutter/material.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/widget/button_widget.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/verification_code_widet.dart';

// enum ConfirmDialogType { error, success }

class ActionModel {
  String label;
  double? width;
  VoidCallback? onPressed;
  String? imageBg;
  Color? colorStart;
  Color? colorEnd;
  Color? fontColor;

  ActionModel({
    required this.label,
    this.onPressed,
    this.width = 120,
    this.imageBg,
    this.colorStart = AppColors.startColor,
    this.colorEnd = AppColors.endColor,
    this.fontColor = AppColors.fontColorBlack12,
  });
}

/// 确认弹窗
/// * [message] 弹窗信息
/// * [messages] 多条换行信息
/// * [fontSize] message的文字大小
/// * [children] 自定义显示内容
/// * [closeButton] 是否显示关闭按钮,默认显示
/// * [barrierDismissible] 是否允许遮罩点击关闭，默认不允许
/// * [onPressed] 单个按钮的额外点击事件，默认都会触发关闭弹窗
/// * [btnLabel] 按钮名称
/// * [ConfirmDialogType] 成功失败的类型
/// * [actions] 按钮组，可定义按钮的名字，样式和点击事件，跟上面一样关闭弹窗不需要手动触发
Future<bool> showConfirmDialog({
  required BuildContext context,
  String? message,
  List<String>? messages,
  double? fontSize = 16,
  double? fontSizeMessages = 12,
  List<Widget>? children,
  bool? closeButton = false,
  bool? showBgColor = true,
  bool? barrierDismissible = false,
  VoidCallback? onPressed,
  String? btnLabel = '确 定',
  String? type,
  bool needClose = true,
  List<ActionModel>? actions,
  bool? inputCode = false,
  bool? showRefreshIco = false,
  Color? fontTitleColor = AppColors.fontColor,
  Color? fontMessagesColor = AppColors.fontColorGray99,
}) async {
  return await showDialog<bool>(
        context: context,
        barrierDismissible: barrierDismissible!,
        builder: (BuildContext context) {
          return PopScope(
            canPop:false,
            child:    Dialog(
            backgroundColor: Colors.transparent,
            elevation: 0,
            child: Stack(
              clipBehavior: Clip.none,
              children: [
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    if (closeButton == true && showBgColor == false)
                      Padding(
                        padding: EdgeInsets.only(top: 12.px, right: 12.px),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            GestureDetector(
                              onTap: () {
                                Navigator.of(context).pop();
                              },
                              child: ImageAssetWidget(
                                imageUrl: 'static/images/close2_btm.png',
                                height: 20.px,
                                width: 20.px,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ConstrainedBox(
                      constraints: const BoxConstraints(minHeight: 100),
                      child: Container(
                        decoration: BoxDecoration(
                          // border: Border.all(color: AppColors.borderGreenD6),
                          gradient: LinearGradient(
                            colors: [
                              showBgColor == true
                                  ? (showRefreshIco == true
                                      ? AppColors.backgroundColor
                                      : AppColors.backgroundColorBlackLight)
                                  : Colors.transparent,
                              showBgColor == true
                                  ? (showRefreshIco == true
                                      ? AppColors.backgroundColor
                                      : AppColors.backgroundColorBlackLight)
                                  : Colors.transparent,
                            ],
                          ),
                          borderRadius: BorderRadius.circular(6.px),
                        ),
                        child: Padding(
                          padding:
                              showRefreshIco == true
                                  ? EdgeInsets.fromLTRB(26, 26, 26, 26)
                                  : EdgeInsets.fromLTRB(16, 16, 16, 16),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              if (type != null) _buildImage(type),
                              if (message != null)
                                _buildText(
                                  message,
                                  'blod',
                                  fontSize,
                                  fontTitleColor,
                                ),
                              if (messages != null)
                                ...messages.map(
                                  (String msg) => _buildText(
                                    msg,
                                    null,
                                    fontSizeMessages,
                                    fontMessagesColor,
                                  ),
                                ),
                              if (children != null) ...children,
                              actions == null
                                  ? _buildDefaultButton(
                                    context,
                                    btnLabel,
                                    onPressed,
                                  )
                                  : Column(
                                    children: [
                                      hiSpace(height: 12.px),
                                      _buildActionsBtn(context, actions,needClose),
                                    ],
                                  ),
                            ],
                          ),
                        ),
                      ),
                    ),
                    closeButton == true && showRefreshIco == true
                        ? hiSpace(height: 20.px)
                        : hiSpace(),
                    if (closeButton == true && showRefreshIco == true)
                      _buildCloseButton(context),
                  ],
                ),
                closeButton == true &&
                        showRefreshIco == false &&
                        showBgColor == true
                    ? Positioned(
                      right: 12.px,
                      top: 12.px,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                        child: ImageAssetWidget(
                          imageUrl: 'static/images/close_dialog.png',
                          height: 20.px,
                          width: 20.px,
                        ),
                      ),
                    )
                    : SizedBox(),
                // closeButton == true && showBgColor == false
                //     ? Positioned(
                //       right: 15.px,
                //       top: -15.px,
                //       child: GestureDetector(
                //         onTap: () {
                //           Navigator.of(context).pop();
                //         },
                //         child: ImageAssetWidget(
                //           imageUrl: 'static/images/close2_btm.png',
                //           height: 20.px,
                //           width: 20.px,
                //         ),
                //       ),
                //     )
                //     : SizedBox(),
                // Positioned(
                //   right: -6.px,
                //   top: showRefreshIco == true ? 34.px : 0,
                //   child: ImageAssetWidget(
                //     imageUrl: 'static/images/leaf_right.png',
                //     height: 48.px,
                //     width: 82.px,
                //   ),
                // ),
                showRefreshIco == true
                    ? Positioned(
                      right: 26.px,
                      top: 28.px,
                      child: GestureDetector(
                        onTap: () {
                          onPressed?.call();
                        },
                        child: ImageAssetWidget(
                          imageUrl: 'static/images/refresh_ico.png',
                          height: 24.px,
                          width: 24.px,
                        ),
                      ),
                    )
                    : SizedBox(),
              ],
            ),
          )
       );
        },
      ) ??
      false;
}

_buildActionsBtn(BuildContext context, List<ActionModel> actions,bool needClose ) {
  return Row(
    mainAxisAlignment:
        actions.length > 1
            ? MainAxisAlignment.spaceBetween
            : MainAxisAlignment.center,
    children:
        actions.map((item) {
          return ButtonWidget(
            label: item.label,
            fontSize: 14.px,
            height: 36,
            width: item.width,
            imageBg: item.imageBg,
            bgColorStart: item.colorStart!,
            bgColorEnd: item.colorEnd!,
            fontColor: item.fontColor!,
            onPressed: () {
              if(needClose==true){
                Navigator.of(context).pop();
              }
            
              item.onPressed?.call();
            },
          );
        }).toList(),
  );
}

_buildImage(String imageName) {
  return Container(
    height: 74.px,
    width: 74.px,
    margin: const EdgeInsets.only(bottom: 8),
    decoration: BoxDecoration(
      image: DecorationImage(image: AssetImage('static/images/$imageName.png')),
    ),
  );
}

_buildText(String msg, String? type, double? fontSize, Color? fontColor) {
  return Column(
    children: [
      Text(
        msg,
        textAlign: TextAlign.center,
        style: TextStyle(
          // color: type == null ? AppColors.fontColor : AppColors.fontColorGray99,
          color: fontColor,
          fontSize: fontSize?.px ?? 18.px,
          fontWeight: type == null ? FontWeight.normal : FontWeight.bold,
        ),
      ),
      hiSpace(height: 10.px),
    ],
  );
}

_buildDefaultButton(
  BuildContext context,
  String? text,
  VoidCallback? onPressed,
) {
  return Padding(
    padding: EdgeInsets.only(bottom: 24.px),
    child: ButtonWidget(
      label: text!,
      height: 44,
      onPressed: () {
        // debugPrint('点击了');
        Navigator.of(context).pop();
        onPressed?.call();
      },
    ),
  );
}

_buildCloseButton(BuildContext context) {
  return Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      GestureDetector(
        onTap: () {
          Navigator.of(context).pop();
        },
        child: ImageAssetWidget(
          imageUrl: 'static/images/close2_btm.png',
          width: 29,
          height: 28,
        ),
      ),
    ],
  );
}
